package com.oracle.lingshangejia.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;

import com.afollestad.materialdialogs.MaterialDialog;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.oracle.children.ChildrenArchivesActivity;
import com.oracle.lingshangejia.Constant;
import com.oracle.lingshangejia.R;
import com.oracle.lingshangejia.activity.special.ChanHou42MainActivity;
import com.oracle.lingshangejia.activity.special.ChanHouActivity;
import com.oracle.lingshangejia.activity.special.ChanJian25Activity;
import com.oracle.lingshangejia.activity.special.ChanjianArchivesActivity;
import com.oracle.lingshangejia.activity.special.ErTong12ArchivesActivity;
import com.oracle.lingshangejia.activity.special.ErTong18ArchivesActivity;
import com.oracle.lingshangejia.activity.special.ErTong36ArchivesActivity;
import com.oracle.lingshangejia.activity.special.ErTongZhongYiActivity;
import com.oracle.lingshangejia.activity.special.HighBloodPpressureActivity;
import com.oracle.lingshangejia.activity.special.LaoNianRenActivity;
import com.oracle.lingshangejia.activity.special.TangNiaoBingActivity;
import com.oracle.lingshangejia.adapter.SpecialManagementAdapter;
import com.oracle.lingshangejia.base.BaseActivity;
import com.oracle.lingshangejia.bean.DataBean;
import com.oracle.lingshangejia.bean.SpecialDiseaseBaen;
import com.oracle.lingshangejia.bean.User;
import com.oracle.lingshangejia.model.OkgoNetwork;
import com.oracle.lingshangejia.utils.MyLogger;
import com.oracle.lingshangejia.utils.MyToast;
import com.oracle.lingshangejia.utils.ObserverOnNext;
import com.oracle.lingshangejia.utils.RxUtils;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;

//专项病
public class SpecialManagementActivity extends BaseActivity implements View.OnClickListener, BaseQuickAdapter.OnItemLongClickListener, BaseQuickAdapter.OnItemClickListener {


    @BindView(R.id.rv)
    RecyclerView mRv;
    private RelativeLayout re_back, re_add;
    private SpecialManagementAdapter mAdapter;
    private int mId;
    private String mTjbh;

    @Override
    public int intiLayout() {
        return R.layout.activity_special_management;
    }

    @Override
    public void initView() {
        re_back = findViewById(R.id.re_back);
        re_back.setOnClickListener(this);
        re_add = findViewById(R.id.re_add);
        //  re_add.setVisibility(View.INVISIBLE);
        re_add.setOnClickListener(this);

    }

    @Override
    public void initData() {
        ButterKnife.bind(this); //获取居民id
        mId = getIntent().getIntExtra("jmid", 0);
        mTjbh = getIntent().getStringExtra("tjbh");
        initRecycelrView();
        getDate();


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //返回键
            case R.id.re_back:
                finish();
                break;
            //添加
            case R.id.re_add:
                getSpecialDisease();
                break;
        }
    }

    //过取专项病标签
    private void getSpecialDisease() {
        showLoading();
        Observable.create(new ObservableOnSubscribe<SpecialDiseaseBaen>() {
            @Override
            public void subscribe(ObservableEmitter<SpecialDiseaseBaen> emitter) throws Exception {
                Map<Object, Object> map = new HashMap<>();
                map.put("token", User.getToken());
                map.put("mark", 1);
                map.put("offset", 0);
                map.put("limit", 20);
                JSONObject jsonObject = new JSONObject(map);
                MyLogger.i(jsonObject.toString());
                Gson gson = new Gson();
                String json = OkgoNetwork.post(Constant.special_disease_list, jsonObject.toString());
                MyLogger.i(json);
                emitter.onNext(gson.fromJson(json, SpecialDiseaseBaen.class));
            }
        }).compose(RxUtils.io_main()).subscribe(new ObserverOnNext<SpecialDiseaseBaen>() {
            @Override
            public void onNext(SpecialDiseaseBaen baen) {
                dismissLoading();
                if (baen.getData() != null && baen.getData().getRows().size() > 0) {
                    List<String> list = new ArrayList<>();
                    for (SpecialDiseaseBaen.DataBean.RowsBean rowsBean : baen.getData().getRows()) {
                        list.add(rowsBean.getName());
                    }
                    new MaterialDialog.Builder(SpecialManagementActivity.this)
                            .title("专项服务")
                            .positiveText("确认")
                            .items(list)
                            .itemsCallbackSingleChoice(-1, new MaterialDialog.ListCallbackSingleChoice() {
                                @Override
                                public boolean onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
                                    if (!TextUtils.isEmpty(text)) {
                                        for (SpecialDiseaseBaen.DataBean.RowsBean rowsBean : baen.getData().getRows()) {
                                            if (rowsBean.getName().equals(text)) {
                                                add(rowsBean.getId());
                                                break;
                                            }
                                        }
                                       // MyToast.show("添加成功");
                                    }


                                    return true;
                                }
                            })
                            .show();


                } else {
                    MyToast.show(baen.getMsg());
                }


            }
        });

    }

    //获取当前用户专项病
    private void getDate() {
        showLoading();
        int id = getIntent().getIntExtra("jmid", 0);
        Observable.create(new ObservableOnSubscribe<SpecialDiseaseBaen>() {
            @Override
            public void subscribe(ObservableEmitter<SpecialDiseaseBaen> emitter) throws Exception {
                Map<Object, Object> map = new HashMap<>();
                map.put("token", User.getToken());
                map.put("mark", 1);
                map.put("userId", id);
                map.put("offset", 0);
                map.put("limit", 20);
                JSONObject jsonObject = new JSONObject(map);
                MyLogger.i(jsonObject.toString());
                Gson gson = new Gson();
                String json = OkgoNetwork.post(Constant.user_special_disease, jsonObject.toString());
                MyLogger.i(json);
                emitter.onNext(gson.fromJson(json, SpecialDiseaseBaen.class));
            }
        }).compose(RxUtils.io_main()).subscribe(new ObserverOnNext<SpecialDiseaseBaen>() {

            @Override
            public void onNext(SpecialDiseaseBaen baen) {
                if (baen.getData() != null && baen.getData().getRows().size() > 0) {
                    mAdapter.setNewData(baen.getData().getRows());
                } else {
                    View emptyView = getLayoutInflater().inflate(R.layout.view_empty, null);
                    mAdapter.setEmptyView(emptyView);
                    MyToast.show("没有专项数据");
                }

                dismissLoading();
            }
        });
    }

    private void initRecycelrView() {
        mRv.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new SpecialManagementAdapter(null);
        mAdapter.setOnItemLongClickListener(this);
        mAdapter.setOnItemClickListener(this);
        mRv.setAdapter(mAdapter);
    }


    private void delete(int id) {
        Observable.create(new ObservableOnSubscribe<DataBean>() {
            @Override
            public void subscribe(ObservableEmitter<DataBean> emitter) throws Exception {
                int jmid = getIntent().getIntExtra("jmid", 0);
                Map<Object, Object> map = new HashMap<>();
                map.put("token", User.getToken());
                map.put("mark", 1);
                map.put("userId", jmid);
                map.put("zxbId", id);
                JSONObject jsonObject = new JSONObject(map);
                MyLogger.i(jsonObject.toString());
                Gson gson = new Gson();
                String json = OkgoNetwork.post(Constant.delete_special_disease, jsonObject.toString());
                MyLogger.i(json);
                emitter.onNext(gson.fromJson(json, DataBean.class));
            }
        }).compose(RxUtils.io_main()).subscribe(new ObserverOnNext<DataBean>() {

            @Override
            public void onNext(DataBean baen) {
                if (baen.getCode() == 0) {
                    MyToast.show("删除成功");
                    getDate();
                } else {
                    MyToast.show(baen.getMsg());
                }

                dismissLoading();
            }
        });

    }

    //添加专项病
    private void add(int id) {
        //showLoading();
        Observable.create(new ObservableOnSubscribe<DataBean>() {
            @Override
            public void subscribe(ObservableEmitter<DataBean> emitter) throws Exception {
                int jmid = getIntent().getIntExtra("jmid", 0);
                Map<Object, Object> map = new HashMap<>();
                map.put("token", User.getToken());
                map.put("mark", 1);
                map.put("userId", jmid);
                map.put("zxbId", id);
                JSONObject jsonObject = new JSONObject(map);
                MyLogger.i(jsonObject.toString());
                Gson gson = new Gson();
                String json = OkgoNetwork.post(Constant.add_special_disease, jsonObject.toString());
                MyLogger.i(json);
                emitter.onNext(gson.fromJson(json, DataBean.class));
            }
        }).compose(RxUtils.io_main()).subscribe(new ObserverOnNext<DataBean>() {

            @Override
            public void onNext(DataBean baen) {
                if (baen.getCode() == 0) {
                    MyToast.show("添加成功");
                    getDate();
                } else {
                    MyToast.show(baen.getMsg());
                }

                // dismissLoading();
            }
        });

    }

    @Override
    public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
        showDeleteDialog(mAdapter.getData().get(position).getId());
        return true;
    }

    //删除
    private void showDeleteDialog(int id) {
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(this);
        normalDialog.setTitle("删除");
        normalDialog.setMessage("是否删除");
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        delete(id);
                        dialog.dismiss();

                    }
                });
        normalDialog.setNegativeButton("取消",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        normalDialog.show();

    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
       /* Intent intent=new Intent(this,HealthCheckArchivesActivity.class);
        intent.putExtra("jmid",mId);
        intent.putExtra("tjbh",mTjbh);//编号
        startActivity(intent);*/
        Intent intent = new Intent();
        intent.putExtra("jmid", mId);
        switch (mAdapter.getData().get(position).getName()) {
            case "新生儿家庭访视记录":
                intent.setClass(this, ChildrenArchivesActivity.class);
                break;
            case "高血压患者随访记录":
                intent.setClass(this, HighBloodPpressureActivity.class);
                break;
            case "2型糖尿病患者健康随访记录":
                intent.setClass(this, TangNiaoBingActivity.class);
                break;
            case "老年人中医药健康管理":
                intent.setClass(this, LaoNianRenActivity.class);
                break;
            case "1到8月龄儿童健康检查记录":
                intent.setClass(this, ErTong18ArchivesActivity.class);
                break;
            case "12到30月龄儿童健康检查记录":
                intent.setClass(this, ErTong12ArchivesActivity.class);
                break;
            case "3到6岁儿童健康检查记录":
                intent.setClass(this, ErTong36ArchivesActivity.class);
                break;
            case "儿童中医药健康管理服务记录":
                intent.setClass(this, ErTongZhongYiActivity.class);
                break;
            case "孕产妇一次产前检查记录":
                intent.setClass(this, ChanjianArchivesActivity.class);
                break;
            case "孕产妇2到5次产前随访服务记录":
                intent.setClass(this, ChanJian25Activity.class);
                break;
            case "孕产妇产后访视记录":
                intent.setClass(this, ChanHouActivity.class);
                break;
            case "孕产妇产后42天健康检查记录":
                intent.setClass(this, ChanHou42MainActivity.class);
                break;
            case "健康体检表":
                intent.setClass(this, HealthCheckArchivesActivity.class);
                break;


        }
        startActivity(intent);
    }



    @OnClick(R.id.iv_special_list)
    public void onViewClicked() {
        Intent intent = new Intent(this,SpecialListActivity.class);
        intent.putExtra("jmid", mId);
        startActivity(intent);
    }
}


















